package com.css.apps.base.common.provider.json;

import java.util.List;

import com.css.apps.base.post.model.SPost;
import com.css.apps.base.role.model.SRole;
import com.css.apps.base.sys.action.SysItem;
import com.css.apps.base.sys.model.SSys;
import com.css.apps.base.user.model.SUser;
import com.css.db.query.QueryCache;
import com.css.ids.server.service.dao.IdsUserDao;
import com.css.util.ListUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class UserJsonProvider {

	public JSONArray getUserOrgTree(String orgId) {
		JSONArray res = new JSONArray();
		List<SUser> allUsers = IdsUserDao.listAllUser();
		if (ListUtil.isLstEmpty(allUsers)) {
			for (SUser user : allUsers) {
				JSONObject u = new JSONObject();
				u.element("id", user.getUuid());
				u.element("name", user.getRealName());
				u.element("deptId", user.getOrgId());
				res.add(u);
			}
		}
		return res;
	}
	
	@SuppressWarnings("unchecked")
	public JSONArray getUserPostTree(String userId) throws JSONException {
		List<String> postids = new QueryCache("select a.postId from SUserPost a where a.userId=:userId")
				.setParameter("userId", userId).list();
		JSONArray jaTree = new JSONArray();
		JSONObject one = new JSONObject();
		one.put("id", "0");
		one.put("name", "所有岗位");
		one.put("pId", "-1");
		one.put("isParent", true);
		one.put("open", true);
		one.put("use", false);
		jaTree.add(one);
		List<String> list = new QueryCache("select a.uuid from SPost a").list();
		List<SPost> lst = QueryCache.idToObj(SPost.class, list);
		if (lst != null)
			for (SPost item : lst) {
				one = new JSONObject();
				one.put("id", item.getUuid());
				one.put("name", item.getName());
				one.put("pId", "0");
				one.put("isParent", false);
				one.put("use", true);
				if (postids.contains(item.getUuid()))
					one.put("checked", true);
				jaTree.add(one);
			}
		return jaTree;
	}
	
	@SuppressWarnings("unchecked")
	public JSONArray getUserRoleTree(String userId) throws JSONException {
		List<String> rids = new QueryCache("select a.roleId from SUserRole a where a.userId=:userId")
				.setParameter("userId", userId).list();
		List<SSys> sysList = SysItem.getSystems();
		JSONArray jsonArray = new JSONArray();
		JSONObject one;
		if (sysList != null)
			for (SSys item : sysList) {
				one = new JSONObject();
				one.put("id", item.getUuid());
				one.put("name", item.getName());
				one.put("pId", "0");
				one.put("sysId", item.getUuid());
				one.put("isParent", true);
				one.put("use", false);
				jsonArray.add(one);
				List<String> list = new QueryCache("select a.uuid from SRole a where a.sysId=:sysId")
						.setParameter("sysId", item.getUuid()).list();
				List<SRole> lst = QueryCache.idToObj(SRole.class, list);
				if (lst != null)
					for (SRole item2 : lst) {
						one = new JSONObject();
						one.put("id", item2.getUuid());
						one.put("name", item2.getName());
						one.put("sysId", item2.getSysId());
						one.put("pId", item2.getSysId());
						one.put("isParent", false);
						one.put("use", true);
						if (rids.contains(item2.getUuid()))
							one.put("checked", true);
						jsonArray.add(one);
					}

			}
		return jsonArray;
	}
	
}
